<?php
/**
 * Created by PhpStorm.
 * User: zwc
 * Date: 2022/2/7
 * Time: 13:39
 */

namespace App\Controller\User;


use App\Dao\Task\TaskDao;
use App\Dao\User\UserDao;
use App\Dao\User\UserSpreadDao;
use App\Dao\User\UserSpreadSettingDao;
use App\Dao\Users\UsersDao;
use App\Middleware\AuthTokenMiddleware;
use App\Middleware\CheckLoginMiddleware;
use App\Service\Task\TaskReceiveService;
use App\Service\Task\TaskService;
use App\Service\Users\UsersService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Middleware;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;

/**
 * Class UserController
 * @ApiController(prefix="user/user")
 * @Middleware(AuthTokenMiddleware::class)
 */
class UserController extends BaseController
{


    /**
     * @Inject()
     * @var UsersDao
     */
    protected $usersDao;


    /**
     * @Inject();
     * @var TaskDao
     */
    protected $taskDao;

    /**
     * @Inject()
     * @var TaskService
     */
    protected $taskService;

    /**
     * @Inject()
     * @var UsersService
     */
    protected $userService;

    /**
     * @Inject()
     * @var TaskReceiveService
     */
    protected $taskReceiveService;

    /**
     * @PostApi(path="detail")
     */
    public function detail()
    {
        $usersId = $this->getUid();
        $detail = $this->usersDao->read($usersId);
        $task_statistics = $this->taskReceiveService->statistics($usersId);
        $detail->task_statistics = $task_statistics;
        _SUCCESS($detail);
    }


    /**
     * @PostApi(path="invoice")
     * @FormData(key="code")
     */
    public function invoice()
    {
        $uid = $this->getUid();
        $this->userService->invoice($uid, $this->getParams()->get('code'));
        _SUCCESS();
    }

    /**
     * @PostApi(path="bind")
     * @FormData(key="code")
     */
    public function bind()
    {
        $params = $this->getParams();
        $user_id = $this->getUid();
        $this->userService->bind($user_id, $params->get('code'));
        _SUCCESS();
    }


    /**
     * @PostApi(path="bind-detail")
     */
    public function bindDetail()
    {
        $user_id = $this->getUid();
        $data = $this->userService->getBindDetail($user_id);
        _SUCCESS($data);
    }

    /**
     * @PostApi(path="invite-detail")
     */
    public function inviteDetail()
    {
        $user_id = $this->getUid();

        //获取邀请好友数量
        $people = App(UserDao::class)->where('spread_uid', $user_id)->count();
        $user = App(UserDao::class)->where('uid', $user_id)->first();

        $setting = App(UserSpreadSettingDao::class)->orderBy('people')->where('people', '>', $people)->limit(2)->get();

        if (is_null($setting)) {
            $wait_people = 0;
            $wait_num = 0;
        } else {
            $wait_people = $setting->first()->toArray()['people'] - $people;
            $wait_num = $setting->first()->toArray()['num'];
        }

        $today_people = App(UserDao::class)->yjRangeTime('spread_time', 'today')->where('spread_uid', $user_id)->count();
        $yesterday_people = App(UserDao::class)->yjRangeTime('spread_time', 'yesterday')->where('spread_uid', $user_id)->count();


        _SUCCESS(compact('user', 'people', 'setting', 'wait_people', 'wait_num', 'today_people', 'yesterday_people'));
    }
}